package com.example.studentinfomanager;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private DBHelper dbHelper;
    private List<Student> studentList = new ArrayList<>();
    private StudentAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dbHelper = new DBHelper(this);

        initListView();

        initButtons();
    }

    private void initListView() {
        ListView listView = findViewById(R.id.lvStudent);
        adapter = new StudentAdapter(this, R.layout.item_student, studentList);
        listView.setAdapter(adapter);
    }

    private void initButtons() {
        findViewById(R.id.btnWrite).setOnClickListener(v -> {
            String name = ((EditText) findViewById(R.id.etName)).getText().toString();
            String stuid = ((EditText) findViewById(R.id.etId)).getText().toString();
            String gender = ((EditText) findViewById(R.id.etGender)).getText().toString();
            String score = ((EditText) findViewById(R.id.etScore)).getText().toString();

            SQLiteDatabase db = dbHelper.getWritableDatabase();
            String sql = "INSERT INTO " + DBHelper.TABLE_NAME + " (name, stuid, gender, score) VALUES (?, ?, ?, ?)";
            db.execSQL(sql, new String[]{name, stuid, gender, score});
            db.close();

            queryData();
            Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
        });

        findViewById(R.id.btnDelete).setOnClickListener(v -> {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            String sql = "DELETE FROM " + DBHelper.TABLE_NAME + " WHERE id = (SELECT MAX(id) FROM " + DBHelper.TABLE_NAME + ")";
            db.execSQL(sql);
            db.close();

            queryData();
            Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
        });

        findViewById(R.id.btnQuery).setOnClickListener(v -> {
            queryData();
            Toast.makeText(this, "查询成功", Toast.LENGTH_SHORT).show();
        });

        findViewById(R.id.btnUpdate).setOnClickListener(v -> {
            String newName = ((EditText) findViewById(R.id.etName)).getText().toString();
            String newStuid = ((EditText) findViewById(R.id.etId)).getText().toString();
            String newGender = ((EditText) findViewById(R.id.etGender)).getText().toString();
            String newScore = ((EditText) findViewById(R.id.etScore)).getText().toString();

            SQLiteDatabase db = dbHelper.getWritableDatabase();
            String sql = "UPDATE " + DBHelper.TABLE_NAME + " SET name=?, stuid=?, gender=?, score=? WHERE id=1";
            db.execSQL(sql, new String[]{newName, newStuid, newGender, newScore});
            db.close();

            queryData();
            Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
        });
    }

    private void queryData() {
        studentList.clear();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_NAME, null, null, null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String stuid = cursor.getString(cursor.getColumnIndex("stuid"));  // 学号
                String gender = cursor.getString(cursor.getColumnIndex("gender"));  // 性别
                String score = cursor.getString(cursor.getColumnIndex("score"));

                Log.d("StudentData", "姓名: " + name
                        + " | 学号: " + stuid
                        + " | 性别: " + gender
                        + " | 分数: " + score);

                studentList.add(new Student(id, name, stuid, gender, score));
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        adapter.notifyDataSetChanged();
    }

}